// 这份代码专门处理逻辑层的操作
const schoolDao = require('../dao/schoolDao');
const powerDao = require('../dao/powerDao');

const schoolService = {
    /*
     * 获取所有院校接口
     */
    async getAllSchool(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await schoolDao.getAllSchool();
            result = { success: true, msg: '获取所有考研院校成功', data: result };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 收藏院校
     */
    async collectSchool(uid, collegeId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await schoolDao.collectSchool(uid, collegeId);
            if (result.insertId) {
                result = { success: true, msg: '收藏院校成功' };
            } else {
                result = { success: false, msg: '已经收藏院校' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 取消收藏院校
     */
    async noCollectSchool(uid, collegeId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await schoolDao.noCollectSchool(uid, collegeId);
            if (result.affectedRows) {
                result = { success: true, msg: '取消收藏院校成功' };
            } else {
                result = { success: false, msg: '没有收藏该院校' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 懒加载获取收藏院校接口
     */
    async getCollectSchool(uid, lastId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let collectSchoolList = await schoolDao.getCollectSchool(uid, lastId);
            result = { success: true, msg: '获取收藏院校成功', data: { collectSchoolList } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取所有专业
     */
    async getAllMajor(collegeId, uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let majorList = await schoolDao.getAllMajor(collegeId);
            result = { success: true, msg: '获取所有专业成功', data: { majorList } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取某院校所有资讯
     */
    async getAllzixun(uid, collegeId, lastId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let zixunList = await schoolDao.getAllzixun(collegeId, lastId);
            result = { success: true, msg: '获取某院校所有资讯成功', data: { zixunList } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取所有资讯
     */
    async getRecommendzixun(uid, lastId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let zixunList = await schoolDao.getRecommendzixun(lastId);
            result = { success: true, msg: '获取所有资讯成功', data: { zixunList } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取院校信息
     */
    async getSchoolInfo(uid, collegeId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await schoolDao.getSchoolInfo(uid, collegeId);
            if (result.length === 0) {
                result = { success: false, msg: '找不到该院校' };
            } else {
                result = { success: true, msg: '获取院校成功', data: result[0] };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取资讯内容
     */
    async getzixunContent(zixunId, uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看院校信息');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            result = await schoolDao.getzixunContent(zixunId);
            if (result.length === 0) {
                result = { success: false, msg: '找不到该资讯信息' };
            } else {
                result = { success: true, msg: '获取资讯内容成功', data: result[0] };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async loadAllSchool(uid, page) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '院校管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await schoolDao.loadAllSchool(page);
            result = { success: true, msg: '获取所有院校成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    }
};

module.exports = schoolService;